package cn.buk.tms.util;

import org.hibernate.validator.HibernateValidator;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import java.util.Set;

/**
 * @author yfdai
 */
public class ValidationUtils {

    private ValidationUtils() {

    }
    /**
     * 使用hibernate的注解来进行验证
     *
     */
    private static final Validator validator = Validation
            .byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

    /**
     * 功能描述: <br>
     * 〈注解验证参数〉
     *
     * @param obj
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static <T> void validate(T obj) throws RuntimeException {
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
        // 抛出检验异常
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation v = constraintViolations.iterator().next();
            throw new RuntimeException(String.format("参数%s: %s", v.getPropertyPath(), v.getMessage()));
        }
    }
}
